unit mainform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, vg_objects, vg_textbox, vg_controls, vg_layouts, vg_listbox,
  vg_scene,kgs_classes,kgs_importer,kgs_indexer,kgs_searcher, vg_memo;

type
  TForm1 = class(TForm)
    vgScene1: TvgScene;
    Root1: TvgBackground;
    StringListBox1: TvgStringListBox;
    Button1: TvgButton;
    editdbname: TvgTextBox;
    edittablename: TvgTextBox;
    labeldb: TvgText;
    btnindex: TvgButton;
    edittofind: TvgTextBox;
    btnfind: TvgButton;
    lbresult: TvgStringListBox;
    ProgressBar1: TvgProgressBar;
    lblprogress: TvgText;
    Memo1: TvgMemo;
    TextBox1: TvgTextBox;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnindexClick(Sender: TObject);
    procedure btnfindClick(Sender: TObject);
    procedure lbresultClick(Sender: TObject);

  private
    { Private declarations }
    procedure onaddxmlnode(sender:Tobject);
    procedure onindexjob(sender:Tobject);
    procedure onindexprogress(sender:Tobject);
  public
    db:TKGSDatabase;
    { Public declarations }
    procedure opendb;
    procedure freedb;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Tform1.opendb;
begin
  db:=TKGSDatabase.create(editdbname.text);
end;

procedure Tform1.onindexjob(sender:Tobject);
var
  indexer:TKGSIndexer;
begin
  indexer:=sender as TKGSIndexer;
  lblprogress.text:=indexer.indexjob;
end;

procedure Tform1.onindexprogress(sender:Tobject);
var
  indexer:TKGSIndexer;
begin
  indexer:=sender as TKGSIndexer;
  //quite if changes less than 1%
  if (indexer.progressnow-progressbar1.value)/indexer.progressmax < 0.01 then exit;

  progressbar1.max:=indexer.progressmax;
  progressbar1.value:=indexer.progressnow;
  application.ProcessMessages;
end;
procedure Tform1.onaddxmlnode(sender:Tobject);
var
  importer:TKGSImporter;
begin
  importer:=sender as TKgsImporter;
  if importer.currentnode mod 128<>0 then exit;
  progressbar1.Max:=importer.xmlnodecount;
  progressbar1.Value:=importer.currentnode;
  application.processmessages;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
  importer:TKGSImporter;
begin
  if not assigned(db) then opendb;
  if not assigned(db) then raise exception.create('error opening db');

  importer:=TKGSImporter.create(db);

  db.createtables(edittablename.text);
  importer.onAddXmlNode:=onAddXmlNode;
  for i:=0 to stringlistbox1.Items.Count-1 do begin
    lblprogress.Text:='importing '+stringlistbox1.Items[i];
    importer.addxml(stringlistbox1.Items[i]);
  end;

  db.updateindexes(edittablename.text);

  importer.free;


end;
procedure TForm1.freedb;
begin
  if assigned(db) then freeandnil(db);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
  freedb;
end;

procedure TForm1.btnindexClick(Sender: TObject);
var
  indexer:TKGSIndexer;
begin
  if not assigned(db) then opendb;
  progressbar1.value:=0;
  
  indexer:=TKGSIndexer.create(db);

  indexer.onindexjob:=onindexjob;
  indexer.onindexprogress:=onindexprogress;
  indexer.build(edittablename.text);

  indexer.free;
end;

procedure TForm1.btnfindClick(Sender: TObject);
var
  searcher:TKGSSearcher;
  i:integer;
  sl:tstringlist;
begin
  if not assigned(db) then opendb;
  searcher:=TKGSSearcher.create(db,edittablename.text);
  searcher.run(edittofind.text);

  sl:=tstringlist.create;
  for i:=0 to  searcher.match.count-1 do begin
    sl.add(format('%.10d',[searcher.match.next]));
  end;

  lbresult.Items.Clear;
  lbresult.Items.AddStrings(sl);
  sl.free;


end;

procedure TForm1.lbresultClick(Sender: TObject);
var
  row:int64;
  hit:int64;
  id,body:widestring;
begin
  if lbresult.itemindex=-1 then exit;
  hit:=strtoint64def(lbresult.Items[lbresult.itemindex],-1);
  if hit=-1 then exit;

  row:=hit div DEFAULTMAXTOKENPERBLOCK;
  db.getbyrowid(row,id,body);


  textbox1.Text:=id;
  memo1.Lines.Text:=body;
end;

end.
